/*     */ package dm.jdbc.util;
/*     */ 
/*     */ import dm.jdbc.desc.GlobalControlCfg;
/*     */ import dm.jdbc.innerData.DmdbLexWord;
/*     */ import dm.jdbc.lex.Yylex;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class EscapeProcessor
/*     */ {
/*  33 */   private ArrayList m_wordList = new ArrayList();
/*  34 */   private boolean m_lexErr = false;
/*  35 */   private boolean m_excptThrow = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String escapePro()
/*     */   {
/*  47 */     DmdbLexWord lexWord3 = null;
/*  48 */     DmdbLexWord lexWord5 = null;
/*  49 */     String addedArg = null;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  54 */     int leftQIndex = -1;
/*  55 */     int wordToEscape = -1;
/*  56 */     int rightQIndex = -1;
/*     */     
/*  58 */     int len = this.m_wordList.size();
/*  59 */     for (int i = 0; i < len; i++)
/*     */     {
/*  61 */       String word = ((DmdbLexWord)this.m_wordList.get(i)).getM_value();
/*  62 */       if (word.equals("}"))
/*     */       {
/*  64 */         rightQIndex = i;
/*     */         
/*  66 */         boolean isEscape = false;
/*  67 */         String word3 = null;
/*     */         
/*  69 */         for (int j = i - 1; j >= 0; j--) {
/*  70 */           String word4 = ((DmdbLexWord)this.m_wordList.get(j)).getM_value();
/*  71 */           if (word4.equals("{"))
/*     */           {
/*  73 */             leftQIndex = j;
/*     */             
/*     */ 
/*  76 */             lexWord3 = (DmdbLexWord)this.m_wordList.get(j + 1);
/*  77 */             while ((lexWord3 != null) && 
/*  78 */               (lexWord3.getM_type() == 7)) {
/*  79 */               lexWord3 = (DmdbLexWord)this.m_wordList.get(++j + 1);
/*     */             }
/*     */             
/*  82 */             word3 = lexWord3.getM_value();
/*  83 */             wordToEscape = j + 1;
/*  84 */             break;
/*     */           }
/*     */         }
/*     */         
/*  88 */         if ((word3 != null) && (lexWord3 != null))
/*     */         {
/*     */ 
/*     */ 
/*  92 */           if (!word3.equalsIgnoreCase("oj"))
/*     */           {
/*  94 */             if (word3.equalsIgnoreCase("d")) {
/*  95 */               lexWord3.setM_value("date");
/*  96 */               lexWord3.setM_valLen("date".length());
/*  97 */               isEscape = true;
/*  98 */             } else if (word3.equalsIgnoreCase("t")) {
/*  99 */               lexWord3.setM_value("time");
/* 100 */               lexWord3.setM_valLen("time".length());
/* 101 */               isEscape = true;
/* 102 */             } else if (word3.equalsIgnoreCase("ts")) {
/* 103 */               lexWord3.setM_value("datetime");
/* 104 */               lexWord3.setM_valLen("datetime".length());
/* 105 */               isEscape = true;
/* 106 */             } else if (word3.equalsIgnoreCase("?"))
/*     */             {
/*     */ 
/* 109 */               int k = wordToEscape + 1;
/* 110 */               lexWord5 = (DmdbLexWord)this.m_wordList.get(k);
/* 111 */               while ((lexWord5 != null) && 
/* 112 */                 (!lexWord5.getM_value().equals("=")) && (
/* 113 */                 k < this.m_wordList.size() - 1)) {
/* 114 */                 lexWord5 = (DmdbLexWord)this.m_wordList.get(++k);
/*     */               }
/*     */               
/* 117 */               if ((lexWord5 != null) && (lexWord5.getM_value().equals("="))) {
/* 118 */                 k++;
/* 119 */                 lexWord5 = (DmdbLexWord)this.m_wordList.get(k);
/* 120 */                 while ((lexWord5 != null) && 
/*     */                 
/* 122 */                   (!lexWord5.getM_value().equalsIgnoreCase("call")) && (k < this.m_wordList.size() - 1)) {
/* 123 */                   lexWord5 = (DmdbLexWord)this.m_wordList.get(++k);
/*     */                 }
/* 125 */                 if (lexWord5 != null)
/*     */                 {
/* 127 */                   if ("call".equalsIgnoreCase(lexWord5.getM_value())) {
/* 128 */                     this.m_wordList.remove(k);
/*     */                     
/*     */ 
/* 131 */                     rightQIndex--;
/*     */                   }
/*     */                 }
/*     */               }
/* 135 */               isEscape = true;
/* 136 */             } else if ((word3.equalsIgnoreCase("escape")) || 
/* 137 */               (word3.equalsIgnoreCase("call"))) {
/* 138 */               isEscape = true;
/* 139 */             } else { if (!word3.equalsIgnoreCase("fn")) continue;
/* 140 */               String new_name = null;
/*     */               
/*     */ 
/* 143 */               int k = wordToEscape + 1;
/* 144 */               lexWord5 = (DmdbLexWord)this.m_wordList.get(k);
/* 145 */               while ((lexWord5 != null) && 
/* 146 */                 (lexWord5.getM_type() == 7) && (
/* 147 */                 k < this.m_wordList.size() - 1)) {
/* 148 */                 lexWord5 = (DmdbLexWord)this.m_wordList.get(++k);
/*     */               }
/* 150 */               String word5 = lexWord5.getM_value();
/*     */               
/* 152 */               if ((word5.equalsIgnoreCase("ascii")) || 
/* 153 */                 (word5.equalsIgnoreCase("bit_length")) || 
/* 154 */                 (word5.equalsIgnoreCase("concat")) || 
/* 155 */                 (word5.equalsIgnoreCase("length")) || 
/* 156 */                 (word5.equalsIgnoreCase("locate")) || 
/* 157 */                 (word5.equalsIgnoreCase("ltrim")) || 
/* 158 */                 (word5.equalsIgnoreCase("replace")) || 
/* 159 */                 (word5.equalsIgnoreCase("rtrim")) || 
/* 160 */                 (word5.equalsIgnoreCase("space")) || 
/*     */                 
/* 162 */                 (word5.equalsIgnoreCase("abs")) || 
/* 163 */                 (word5.equalsIgnoreCase("acos")) || 
/* 164 */                 (word5.equalsIgnoreCase("asin")) || 
/* 165 */                 (word5.equalsIgnoreCase("atan")) || 
/* 166 */                 (word5.equalsIgnoreCase("atan2")) || 
/* 167 */                 (word5.equalsIgnoreCase("cos")) || 
/* 168 */                 (word5.equalsIgnoreCase("cot")) || 
/* 169 */                 (word5.equalsIgnoreCase("degrees")) || 
/* 170 */                 (word5.equalsIgnoreCase("floor")) || 
/* 171 */                 (word5.equalsIgnoreCase("log10")) || 
/* 172 */                 (word5.equalsIgnoreCase("mod")) || 
/* 173 */                 (word5.equalsIgnoreCase("pi")) || 
/* 174 */                 (word5.equalsIgnoreCase("power")) || 
/* 175 */                 (word5.equalsIgnoreCase("radians")) || 
/* 176 */                 (word5.equalsIgnoreCase("round")) || 
/* 177 */                 (word5.equalsIgnoreCase("sign")) || 
/* 178 */                 (word5.equalsIgnoreCase("sin")) || 
/* 179 */                 (word5.equalsIgnoreCase("sqrt")) || 
/* 180 */                 (word5.equalsIgnoreCase("tan")) || 
/* 181 */                 (word5.equalsIgnoreCase("truncate")) || 
/*     */                 
/* 183 */                 (word5.equalsIgnoreCase("dayname")) || 
/* 184 */                 (word5.equalsIgnoreCase("dayofyear")) || 
/* 185 */                 (word5.equalsIgnoreCase("dayofmonth")) || 
/* 186 */                 (word5.equalsIgnoreCase("monthname")) || 
/* 187 */                 (word5.equalsIgnoreCase("quarter")) || 
/* 188 */                 (word5.equalsIgnoreCase("week")) || 
/* 189 */                 (word5.equalsIgnoreCase("year")) || 
/* 190 */                 (word5.equalsIgnoreCase("month")) || 
/* 191 */                 (word5.equalsIgnoreCase("hour")) || 
/* 192 */                 (word5.equalsIgnoreCase("minute")) || 
/* 193 */                 (word5.equalsIgnoreCase("second")) || 
/*     */                 
/* 195 */                 (word5.equalsIgnoreCase("user")))
/*     */               {
/*     */ 
/* 198 */                 new_name = word5;
/* 199 */               } else if (word5.equalsIgnoreCase("char")) {
/* 200 */                 new_name = "chr";
/* 201 */               } else if (word5.equalsIgnoreCase("char_length")) {
/* 202 */                 new_name = "length";
/* 203 */               } else if (word5.equalsIgnoreCase("character_length")) {
/* 204 */                 new_name = "length";
/* 205 */               } else if (word5.equalsIgnoreCase("insert")) {
/* 206 */                 new_name = "insstr";
/* 207 */               } else if (word5.equalsIgnoreCase("lcase")) {
/* 208 */                 new_name = "lower";
/* 209 */               } else if (word5.equalsIgnoreCase("left")) {
/* 210 */                 new_name = "leftstr";
/* 211 */               } else if (word5.equalsIgnoreCase("octet_length")) {
/* 212 */                 new_name = "length";
/* 213 */               } else if (word5.equalsIgnoreCase("repeat")) {
/* 214 */                 new_name = "repeatstr";
/* 215 */               } else if (word5.equalsIgnoreCase("right")) {
/* 216 */                 new_name = "rightstr";
/* 217 */               } else if (word5.equalsIgnoreCase("substring")) {
/* 218 */                 new_name = "substr";
/* 219 */               } else if (word5.equalsIgnoreCase("ucase")) {
/* 220 */                 new_name = "upper";
/*     */ 
/*     */               }
/* 223 */               else if (word5.equalsIgnoreCase("ceiling")) {
/* 224 */                 new_name = "ceil";
/* 225 */               } else if (word5.equalsIgnoreCase("log")) {
/* 226 */                 new_name = "ln";
/*     */ 
/*     */               }
/* 229 */               else if (word5.equalsIgnoreCase("current_timestamp")) {
/* 230 */                 new_name = "sysdate";
/* 231 */               } else if (word5.equalsIgnoreCase("dayofmonth")) {
/* 232 */                 addedArg = ",'DD'";
/*     */                 
/* 234 */                 new_name = "datepart";
/* 235 */               } else if (word5.equalsIgnoreCase("dayofweek")) {
/* 236 */                 new_name = "weekday";
/* 237 */               } else if (word5.equalsIgnoreCase("hour")) {
/* 238 */                 addedArg = ",'HH'";
/*     */                 
/* 240 */                 new_name = "datepart";
/* 241 */               } else if (word5.equalsIgnoreCase("minute")) {
/* 242 */                 addedArg = ",'MI'";
/*     */                 
/* 244 */                 new_name = "datepart";
/* 245 */               } else if (word5.equalsIgnoreCase("month")) {
/* 246 */                 addedArg = ",'MM'";
/*     */                 
/* 248 */                 new_name = "datepart";
/* 249 */               } else if (word5.equalsIgnoreCase("now")) {
/* 250 */                 new_name = "sysdate";
/* 251 */               } else if (word5.equalsIgnoreCase("second")) {
/* 252 */                 addedArg = ",'SS'";
/*     */                 
/* 254 */                 new_name = "datepart";
/* 255 */               } else if (word5.equalsIgnoreCase("year")) {
/* 256 */                 addedArg = ",'YYYY'";
/*     */                 
/* 258 */                 new_name = "datepart";
/*     */ 
/*     */               }
/* 261 */               else if (word5.equalsIgnoreCase("ifnull")) {
/* 262 */                 new_name = "nvl";
/* 263 */               } else if (word5.equalsIgnoreCase("database")) {
/* 264 */                 new_name = "cur_database";
/* 265 */               } else if (word5.equalsIgnoreCase("convert")) {
/* 266 */                 new_name = word5;
/*     */ 
/*     */               }
/*     */               else
/*     */               {
/*     */ 
/* 272 */                 new_name = word5;
/*     */               }
/*     */               
/*     */ 
/* 276 */               if (!word5.equals(new_name)) {
/* 277 */                 lexWord5.setM_value(new_name);
/* 278 */                 lexWord5.setM_valLen(new_name.length());
/*     */               }
/*     */               
/* 281 */               if (addedArg != null) {
/* 282 */                 DmdbLexWord addLexWord = new DmdbLexWord(addedArg, 
/* 283 */                   1, addedArg.length());
/* 284 */                 this.m_wordList.add(i - 1, addLexWord);
/* 285 */                 addedArg = null;
/*     */               }
/*     */             }
/*     */           }
/*     */           
/*     */ 
/* 291 */           if ((leftQIndex != -1) && (rightQIndex != -1) && (wordToEscape != -1))
/*     */           {
/*     */ 
/*     */ 
/* 295 */             this.m_wordList.remove(rightQIndex);
/* 296 */             if (!isEscape)
/* 297 */               this.m_wordList.remove(wordToEscape);
/* 298 */             this.m_wordList.remove(leftQIndex);
/*     */             
/*     */ 
/* 301 */             i = 0;
/* 302 */             len = this.m_wordList.size();
/* 303 */             leftQIndex = -1;
/* 304 */             rightQIndex = -1;
/* 305 */             wordToEscape = -1;
/*     */           }
/*     */         }
/*     */       } }
/* 309 */     StringBuffer sqlStr = new StringBuffer();
/* 310 */     len = this.m_wordList.size();
/*     */     
/* 312 */     for (i = 0; i < len; i++) {
/* 313 */       String word = ((DmdbLexWord)this.m_wordList.get(i)).getM_value();
/* 314 */       sqlStr.append(word);
/*     */     }
/*     */     
/* 317 */     return sqlStr.toString();
/*     */   }
/*     */   
/*     */   public synchronized String escapeSQL(String sql, boolean escape, boolean execOpt) throws SQLException
/*     */   {
/* 322 */     boolean doEscape = false;
/* 323 */     if ((escape) && (sql.indexOf("{") != -1) && (sql.indexOf("}") != -1)) {
/* 324 */       doEscape = true;
/*     */     }
/* 326 */     if ((!doEscape) && (!GlobalControlCfg.getKeywordOpen()) && (!execOpt)) {
/* 327 */       return sql;
/*     */     }
/* 329 */     boolean hasOptimize = false;
/*     */     
/* 331 */     if ((doEscape) && (!GlobalControlCfg.getKeywordOpen()) && (!execOpt)) {
/* 332 */       int leftIndex = sql.indexOf("{");
/* 333 */       int rightIndex = sql.lastIndexOf("}");
/* 334 */       if ((leftIndex != -1) && (sql.substring(leftIndex).indexOf("'") == -1) && 
/* 335 */         (rightIndex == -1) && (leftIndex < rightIndex) && 
/* 336 */         (sql.substring(rightIndex, sql.length()).indexOf("'") == -1))
/*     */       {
/* 338 */         hasOptimize = true;
/* 339 */         DmdbLexWord startWord = new DmdbLexWord(sql.substring(0, 
/* 340 */           leftIndex), 1, leftIndex + 1);
/* 341 */         this.m_wordList.add(startWord);
/*     */         try {
/* 343 */           parseSQL(sql.substring(leftIndex, rightIndex + 1), 
/* 344 */             doEscape, this.m_wordList);
/*     */         } catch (SQLException sqle) {
/* 346 */           this.m_lexErr = true;
/* 347 */           if (this.m_excptThrow)
/* 348 */             throw sqle;
/*     */         }
/* 350 */         DmdbLexWord endWord = new DmdbLexWord(
/* 351 */           sql.substring(rightIndex + 1), 1, 
/* 352 */           sql.substring(rightIndex + 1).length());
/* 353 */         this.m_wordList.add(endWord);
/*     */       }
/*     */     }
/*     */     
/* 357 */     if (!hasOptimize) {
/*     */       try
/*     */       {
/* 360 */         parseSQL(sql, doEscape, this.m_wordList);
/*     */       } catch (SQLException sqle) {
/* 362 */         this.m_lexErr = true;
/* 363 */         if (this.m_excptThrow) {
/* 364 */           throw sqle;
/*     */         }
/*     */       }
/*     */     }
/* 368 */     if ((this.m_lexErr) || (this.m_wordList.size() == 0)) {
/* 369 */       return sql;
/*     */     }
/* 371 */     String sqlStr = "";
/* 372 */     if (doEscape) {
/* 373 */       sqlStr = escapePro();
/*     */     } else {
/* 375 */       StringBuffer strBuf = new StringBuffer();
/* 376 */       int len = this.m_wordList.size();
/* 377 */       for (int i = 0; i < len; i++) {
/* 378 */         strBuf.append(((DmdbLexWord)this.m_wordList.get(i)).getM_value());
/*     */       }
/* 380 */       sqlStr = strBuf.toString();
/*     */     }
/*     */     
/* 383 */     return sqlStr;
/*     */   }
/*     */   
/*     */   public boolean hasError() {
/* 387 */     return this.m_lexErr;
/*     */   }
/*     */   
/*     */   private void parseSQL(String sql, boolean escapePro, ArrayList wordList) throws SQLException
/*     */   {
/* 392 */     StringReader reader = new StringReader(sql);
/* 393 */     Yylex lexer = new Yylex(reader, "", this.m_wordList, escapePro);
/* 394 */     DmdbLexWord lexWordTemp = null;
/*     */     try
/*     */     {
/* 397 */       if (GlobalControlCfg.getKeywordOpen()) {
/* 398 */         while (lexer.yylex() > 0) {
/* 399 */           lexWordTemp = (DmdbLexWord)wordList
/* 400 */             .get(wordList.size() - 1);
/* 401 */           String str = lexWordTemp.getM_value();
/* 402 */           String str2 = GlobalControlCfg.repaceReservedWord(str);
/* 403 */           lexWordTemp.setM_value(str2);
/* 404 */           lexWordTemp.setM_valLen(str2.length());
/*     */         }
/*     */       }
/* 407 */       while (lexer.yylex() > 0) {}
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 411 */       throw new SQLException(e.getMessage());
/*     */     }
/*     */     
/* 414 */     if (lexer.getErrCode() < 0)
/* 415 */       throw new SQLException(lexer.getErrMsg());
/*     */   }
/*     */   
/*     */   public void setExcpThrow(boolean toThrow) {
/* 419 */     this.m_excptThrow = toThrow;
/*     */   }
/*     */   
/*     */   public ArrayList getWordList() {
/* 423 */     return this.m_wordList;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\util\EscapeProcessor.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */